<!--
 * @version:
 * @Company:
 * @Description:
 * @FilePath: \src\views\tool\tpl\modal\editor\index.vue
-->
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" width="70%" :keyboard="false" :maskClosable="false" @ok="handleSubmit" okText="对比代码">
    <BasicForm @register="registerForm">
      <template #content="{ model, field }">
        <monaco-editor ref="monacoEditor" v-model="model[field]" lang="tpl" />
      </template>
    </BasicForm>
    <template #centerFooter>
       <a-button type="primary" @click="toSave">保 存</a-button>
       </template>
  </BasicModal>
  <diffModal @register="diffRegisterModal" @success="toSave()" />
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { getFormConfig } from './config';
  import monacoEditor from '/@/components/Custom/MonacoEditor/index.vue';
  import * as _API from '../../api';
  import { Modal } from 'ant-design-vue';
  import diffModal from '../diff/index.vue';
  export default defineComponent({
    name: 'tplForm',
    components: { BasicModal, BasicForm, monacoEditor, diffModal },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const monacoEditor: any = ref(null);
      const isUpdate = ref(false);
      const visible = ref(false);
      const form: any = ref({});
      const [registerForm, { resetFields, setFieldsValue, validate, setProps, getFieldsValue }] = useForm({
        labelWidth: 100,
        schemas: getFormConfig(isUpdate.value),
        showActionButtonGroup: false,
      });
      const [diffRegisterModal, diffModalMethods] = useModal();
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        visible.value = true;
        form.value = data.record;
        await resetFields();
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;
        await setProps({
          schemas: getFormConfig(isUpdate.value),
        });
        if (unref(isUpdate)) {
          await setFieldsValue({
            ...data.record,
          });
        } else {
          await setFieldsValue({ parentPath: data.record.path });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增模板/文件夹' : '编辑模板/文件夹'));

      const toSave = async () => {
        try {
          const value = await validate();
          setModalProps({ confirmLoading: true });
          if (!isUpdate.value) {
            await _API.addFileAPI({ ...getFieldsValue() });
          } else {
            await _API.editFileAPI({ content: getFieldsValue()?.content ?? value?.content, path: form.value.path });
          }
          closeModal();
          emit('success');
        } catch {}
      };
      const handleSubmit = () => {
        Modal.confirm({
          title: '提示',
          content: '是否需要进行代码对比？',
          okText: '确认',
          cancelText: '取消并保存',
          onOk: () => {
            diffModalMethods.openModal(true, { diffValue: form.value.content, modelValue: monacoEditor.value.getVal() || getFieldsValue()?.content });
          },
          onCancel: () => {
            toSave();
          },
        });
      };
      return { registerModal, registerForm, getTitle, toSave, visible, handleSubmit, form, diffRegisterModal, diffModalMethods, monacoEditor };
    },
  });
</script>
